#
# @lc app=leetcode.cn id=337 lang=python3
#
# [337] 打家劫舍 III
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rob(self, root: Optional[TreeNode]) -> int:
        def dfs(node):
            if node is None:
                return 0, 0
            left_rob, left_not_rob = dfs(node.left)
            right_rob, right_not_rob = dfs(node.right)
            node_rob = node.val + left_not_rob + right_not_rob
            node_not_rob = max(left_rob, left_not_rob) + max(right_rob, right_not_rob)
            
            return node_rob, node_not_rob
        return max(dfs(root))
# @lc code=end

